package com.probiz.estore.content.dao.impl;

import java.util.List;

import com.probiz.estore.common.model.content.Subscriber;
import com.probiz.estore.content.dao.SubscriberDao;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.search.SearchCriteria;

/**
 * Dao implementation for Subscriber.
*/
public class SubscriberDaoImpl extends HibernateGenericDaoImpl<Subscriber> implements SubscriberDao {
	
	/**
	 * 获取指定email所订阅的消息组ID列表
	 * @param email
	 * @return
	 */
	public List<Integer> getSubscribedNewsletterGroupIdsByEmail(String email){
		String hql = "select s.newsletterGroup.id from Subscriber s where s.email=?";
		return this.findByHql(hql, email);
	}
	
	/**
	 * 获取分页email列表
	 * @param pageNo
	 * @param pageSize
	 * @param newsletterGroupIds 所在消息组id列表
	 * @return
	 */
	public List<String> searchEmails(int pageNo, int pageSize, List<String> newsletterGroupIds){
		StringBuilder hql = new StringBuilder("select distinct s.email from Subscriber s where s.newsletterGroup.newsletterGroupId in (");
		if(newsletterGroupIds!=null && newsletterGroupIds.size()>0){
			for(String id: newsletterGroupIds){
				hql.append(id).append(','); 
			}
			hql.replace(hql.length()-1, hql.length(), ")");
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql.toString(), new Object[]{}, pageNo, pageSize, null);
			return this.searchByCriteria(sc);
		}
		
		return null;
	}
}
